<template>
  <el-form ref="ruleForm" :model="ruleForm" status-icon :rules="rules" label-width="100px" class="demo-ruleForm">
    <el-form-item label="原密码" prop="oldPass">
      <el-input v-model="ruleForm.oldPass" type="password" autocomplete="off" />
    </el-form-item>
    <el-form-item label="新密码" prop="newPass">
      <el-input v-model="ruleForm.newPass" type="password" autocomplete="off" />
    </el-form-item>
    <el-form-item label="确认密码" prop="checkPass">
      <el-input v-model="ruleForm.checkPass" type="password" autocomplete="off" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  name: 'Password',
  props: {
    user: {
      default: () => {
        return {
          uid: '',
          name: '',
          avatar: '',
          password: ''
        }
      }
    }
  },
  data() {
    var validateOldPass = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('原密码不能为空'))
      }
      setTimeout(() => {
        if (value !== this.user.password) {
          callback(new Error('原密码错误'))
        }
        callback()
      }, 1000)
    }
    var validateNewPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.newPass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        oldPass: '',
        newPass: '',
        checkPass: ''
      },
      rules: {
        oldPass: [
          { validator: validateOldPass, trigger: 'blur' }
        ],
        newPass: [
          { validator: validateNewPass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass, trigger: 'blur' }
        ]
      },
      uid_newPassword: {
        uid: this.$store.state.user.uid,
        password: this.user.password
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$store.state.user.password = this.ruleForm.newPass
          this.uid_newPassword.password = this.ruleForm.newPass
          this.$store.dispatch('user/changepassword', this.uid_newPassword).then(() => {
            this.$message({
              message: '密码修改成功，请重新登录',
              type: 'success',
              duration: 5 * 1000
            })
            this.logout()
          })
        } else {
          return false
        }
      })
    },
    async logout() {
      await this.$store.dispatch('user/logout')
      this.$router.push(`/login?redirect=${this.$route.fullPath}`)
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
